<template>
  <div class="app-container">
    <div class="filter-container">
      <el-button class="filter-item" type="primary" style="margin-right:10px;" @click="onSubscribe"> 订阅 </el-button>
      <el-tag
        v-for="tag in dynamicTags"
        :key="tag"
        closable
        size="medium"
        :disable-transitions="false"
        @close="handleClose(tag)"
      >
        {{ tag }}
      </el-tag>
      <el-input
        v-if="inputVisible"
        ref="saveTagInput"
        v-model="inputValue"
        class="input-new-tag"
        size="small"
        @keyup.enter.native="handleInputConfirm"
        @blur="handleInputConfirm"
      />
      <el-button v-else class="button-new-tag" size="small" @click="showInput">+ New Tag</el-button>
    </div>
    <div class="filter-container">
      <el-button class="filter-item" type="primary" style="margin-right:10px;" @click="onPublish"> 发送 </el-button>
      <span class="filter-item text"> 主题: </span>
      <el-input v-model="send.topic" class="filter-item" style="width:400px;" />
      <span class="filter-item text"> 消息: </span>
      <el-input v-model="send.message" class="filter-item" style="width:400px;" />
      <span class="filter-item text"> 质量: </span>
      <el-select v-model="send.qos" class="filter-item" placeholder="请选择">
        <el-option
          v-for="item in qosList"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        />
      </el-select>
    </div>
    <el-row :gutter="40">
      <el-col :xs="12" :sm="12" :lg="12">
        <div class="filter-container">
          <span class="filter-item text"> 消息发送 </span>
          <el-button class="filter-item" type="danger" style="margin-right:10px;" @click="sendList=[]"> 清除 </el-button>
        </div>
        <el-table
          :data="sendList"
          highlight-current-row
          style="width: 100%;"
          max-height="500"
          :default-sort="{prop: 'time', order: 'descending'}"
        >
          <el-table-column type="index" width="50" />
          <el-table-column label="时间" align="center" prop="time" />
          <el-table-column label="主题" align="center" prop="topic" />
          <el-table-column label="消息" align="center" prop="message" />
        </el-table>
      </el-col>
      <el-col :xs="12" :sm="12" :lg="12">
        <div class="filter-container">
          <span class="filter-item text"> 消息发送 </span>
          <el-button class="filter-item" type="danger" style="margin-right:10px;" @click="recvList=[]"> 清除 </el-button>
        </div>
        <el-table
          :data="recvList"
          highlight-current-row
          style="width: 100%;"
          max-height="500"
          :default-sort="{prop: 'time', order: 'descending'}"
        >
          <el-table-column type="index" width="50" />
          <el-table-column label="时间" align="center" prop="time" />
          <el-table-column label="主题" align="center" prop="topic" />
          <el-table-column label="消息" align="center" prop="message" />
        </el-table>
      </el-col>
    </el-row>

  </div>
</template>

<script>
import { parseTime } from '@/utils'

export default {
  name: 'Device',
  directives: { },
  data() {
    return {

      sendList: [],
      recvList: [],

      dynamicTags: ['fsc/#', '$SYS/brokers/+/clients/#'],
      inputVisible: false,
      inputValue: '',

      qosList: [
        {
          value: 0,
          label: 0
        },
        {
          value: 1,
          label: 1
        },
        {
          value: 2,
          label: 2
        }
      ],

      send: {
        topic: 'fsc/xxx/xxx/tx',
        message: '{"code":"1000","msg":"0"}',
        qos: 0
      }
    }
  },
  created() {
    this.onSubscribe()
  },
  methods: {
    handleClose(tag) {
      this.$mqtt.unsubscribe(tag)
      this.$message({ type: 'success', message: '取消订阅：' + tag })
      this.dynamicTags.splice(this.dynamicTags.indexOf(tag), 1)
    },

    showInput() {
      this.inputVisible = true
      this.$nextTick(_ => {
        this.$refs.saveTagInput.$refs.input.focus()
      })
    },

    handleInputConfirm() {
      const inputValue = this.inputValue
      if (inputValue) {
        this.dynamicTags.push(inputValue)
        // 订阅
        this.$mqtt.subscribe(inputValue)
        this.$message({ type: 'success', message: '订阅：' + inputValue })
      }
      this.inputVisible = false
      this.inputValue = ''
    },

    onSubscribe() {
      for (var i = 0; i < this.dynamicTags.length; i++) {
        this.$mqtt.subscribe(this.dynamicTags[i])
      }
      this.$message({ type: 'success', message: this.dynamicTags })
    },
    onPublish() {
      this.$mqtt.publish(this.send.topic, this.send.message, this.send.qos)
      var nData = new Date()
      this.sendList.push({
        topic: '' + this.send.topic,
        message: '' + this.send.message,
        time: parseTime(nData)
      })
    },

    subscribe(val) {
      console.log('订阅主题')
      console.log(val)
      this.$mqtt.subscribe(val)
    },

    publish(topic, message) {
      console.log('>>>>>>>>>>>>>推送消息')
      console.log('topic:' + topic)
      console.log('message:' + message)
      this.$mqtt.publish(topic, message)
    }
  },
  mqtt: {
    '#'(data, topic) {
      console.log('<<<<<<<<<<<<<<收到消息')
      console.log('topic:' + topic)
      console.log('payload: ' + String.fromCharCode.apply(null, data))

      var nData = new Date()
      this.recvList.push({
        topic: '' + topic,
        message: '' + String.fromCharCode.apply(null, data),
        time: parseTime(nData)
      })
    }
  }

}
</script>

<style lang="scss" scoped>

.text {
    font-size: 20px;
    color: black;
    font-weight: bold;
    margin-left: 10px;
    margin-right: 10px;
}

 .el-tag + .el-tag {
    margin-left: 10px;
  }
  .button-new-tag {
    margin-left: 10px;
    height: 32px;
    line-height: 30px;
    padding-top: 0;
    padding-bottom: 0;
  }
  .input-new-tag {
    width: 90px;
    margin-left: 10px;
    vertical-align: bottom;
  }

</style>
